package view;

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.RowSpec;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import com.jgoodies.forms.factories.FormFactory;

import controller.MainController;

import javax.swing.JLabel;
import javax.swing.JComboBox;
import javax.swing.JPasswordField;

import model.Benutzer;
import javax.swing.ImageIcon;

public class LoginDialog extends JDialog {

	private Benutzer benutzer;
	private MainController mainController;

	private final JPanel panelMitte = new JPanel();
	private JComboBox comboBoxBenutzer;
	private JPasswordField textFieldPasswort;

	/**
	 * Create the dialog.
	 */
	public LoginDialog(MainController mainController) {
		setResizable(false);
		benutzer = null;
		this.mainController = mainController;
		
		setModal(true);
		setTitle("Login");
		setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 300, 200);
		getContentPane().setLayout(new BorderLayout());
		{
			JPanel panelOben = new JPanel();
			getContentPane().add(panelOben, BorderLayout.NORTH);
			{
				JLabel labelLogo = new JLabel("");
				labelLogo.setIcon(new ImageIcon(LoginDialog.class.getResource("/images/Logo.png")));
				panelOben.add(labelLogo);
			}
		}
		panelMitte.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(panelMitte, BorderLayout.CENTER);
		panelMitte.setLayout(new FormLayout(new ColumnSpec[] {
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("default:grow"),},
			new RowSpec[] {
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,}));
		{
			JLabel labelBenutzer = new JLabel("Benutzer");
			panelMitte.add(labelBenutzer, "2, 2, right, default");
		}
		{
			comboBoxBenutzer = new JComboBox(this.mainController.getBenutzerController().getBenutzer().toArray());
			panelMitte.add(comboBoxBenutzer, "4, 2, fill, default");
		}
		{
			JLabel labelPasswort = new JLabel("Passwort");
			panelMitte.add(labelPasswort, "2, 4, right, default");
		}
		{
			textFieldPasswort = new JPasswordField();
			panelMitte.add(textFieldPasswort, "4, 4, fill, default");
		}
		{
			JPanel panelUnten = new JPanel();
			panelUnten.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(panelUnten, BorderLayout.SOUTH);
			{
				JButton buttonOK = new JButton("OK");
				buttonOK.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent arg0) {
						Benutzer benutzer = (Benutzer) comboBoxBenutzer.getSelectedItem();
						int benutzerId = (benutzer == null) ? -1 : benutzer.getBenutzerId();
						
						String passwort = new String(textFieldPasswort.getPassword());
						
						if (LoginDialog.this.mainController.getBenutzerController().loginBenutzer(benutzerId, passwort)) {
							LoginDialog.this.benutzer = benutzer;
							
							setVisible(false);
							dispose();
						}
						else {
							JOptionPane.showMessageDialog(null, "Falsches Passwort");
						}
					}
				});
				buttonOK.setActionCommand("OK");
				panelUnten.add(buttonOK);
				getRootPane().setDefaultButton(buttonOK);
			}
			{
				JButton buttonAbbrechen = new JButton("Abbrechen");
				buttonAbbrechen.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent arg0) {
						setVisible(false);
						dispose();
					}
				});
				buttonAbbrechen.setActionCommand("Cancel");
				panelUnten.add(buttonAbbrechen);
			}
		}
	}

	public Benutzer showDialog() {
		setVisible(true);
		return benutzer;
	}

}
